package com.leetcode.Stack;

import java.util.Stack;

public class Leetcode20 {

    public boolean isValid(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }
        char[] str = s.toCharArray();
        Stack<Character> ch = new Stack<>();
        char begin = str[0];
        if (begin == ')' || begin == '}' || begin == ']') {
            return false;
        }
        ch.push(begin);
        for (int i = 1; i < str.length; i++) {
            if (str[i] == '(' || str[i] == '{' || str[i] == '[') {
                ch.push(str[i]);
            } else {
                if (ch.isEmpty()) {
                    return false;
                }
                char cur = str[i];
                char stackValue = ch.pop();
                if (cur == ')' && stackValue != '(') {
                    return false;
                }
                if (cur == ']' && stackValue != '[') {
                    return false;
                }
                if (cur == '}' && stackValue != '{') {
                    return false;
                }
            }
        }
        return ch.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new Leetcode20().isValid("()]"));

    }

}
